package com.example.huadi1.service.Imp;

import com.example.huadi1.dto.TeacherGetBook;
import com.example.huadi1.dto.Textbook;
import com.example.huadi1.mapper.CourseMapper;
import com.example.huadi1.mapper.TeacherGetBookMapper;
import com.example.huadi1.mapper.TextbookMapper;
import com.example.huadi1.service.TeacherGetBookService;
import com.example.huadi1.service.TextbookService;
import com.example.huadi1.utils.AjaxResult;
import com.example.huadi1.utils.PageUtils;
import com.example.huadi1.utils.Request.GetAllBook;
import com.example.huadi1.utils.Request.GetBookRequest;
import com.example.huadi1.utils.Request.QueryBookRequest;
import com.example.huadi1.utils.Response.ClassGetBook;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class TeacherGetBookImp implements TeacherGetBookService {
    @Autowired
    TeacherGetBookMapper teacherGetBookMapper;
    @Autowired
    CourseMapper courseMapper;
    @Autowired
    TextbookMapper textbookMapper;
    @Autowired
    TextbookService textbookService;

    public AjaxResult GenerateTeacherGetBook(Integer teacherId){
        List<String> courseNameByTeacherId = courseMapper.getCourseNameByTeacherId(teacherId);
        for(String courseName:courseNameByTeacherId){
            List<Integer> textbookIdsByCourseName = textbookMapper.findTextbookIdsByCourseName(courseName);
            for(Integer textbookId:textbookIdsByCourseName){
                teacherGetBookMapper.insertTeacherGetBook(new com.example.huadi1.dto.TeacherGetBook(textbookId,teacherId,0));
            }
        }
        return AjaxResult.success();
    }
    public AjaxResult GenerateAllTeacherGetBook(){
        List<Integer> AllTeacherIds=courseMapper.getAllTeacherId();
        for (Integer teacherId:AllTeacherIds) {
            GenerateTeacherGetBook(teacherId);
        }
        return AjaxResult.success();
    }

    public AjaxResult getBook(GetBookRequest getBookRequest){
        TeacherGetBook teacherGetBook=new TeacherGetBook(getBookRequest.getTextbookId(),getBookRequest.getUserId(),getBookRequest.getTextbookNum());
        teacherGetBookMapper.updateTeacherGetBook(teacherGetBook);
        Textbook textbook=textbookMapper.findById(teacherGetBook.getTextbookId());
        textbook.setStock(textbook.getStock()-1);
        textbookService.updateTextbook(textbook);
        return AjaxResult.success();
    }

    public AjaxResult queryBook(QueryBookRequest queryBookRequest){
        List<TeacherGetBook> teacherGetBooks=teacherGetBookMapper.selectTeacherGetBookByUserId(queryBookRequest.getUserId());
        List<ClassGetBook> getBooks=new ArrayList<>();
        for(TeacherGetBook teacherGetBook:teacherGetBooks){
            Integer canGet=1;
            if(teacherGetBook.getGetNum()>0) canGet=0;
            String courseName=textbookMapper.findById(teacherGetBook.getTextbookId()).getCourseName();
            getBooks.add(new ClassGetBook(teacherGetBook.getTeacherId(),queryBookRequest.getRole(),courseName,teacherGetBook.getTextbookId(),canGet));
        }
        PageHelper.startPage(queryBookRequest.getPageNum(), queryBookRequest.getPageSize());
        PageInfo<ClassGetBook> pageAll = PageInfo.of(getBooks);
        PageUtils pageUtils = new PageUtils(pageAll);
        AjaxResult result=AjaxResult.success(pageUtils);
        return result;
    }
    public AjaxResult getAllTeacherBook(GetAllBook getAllBook){
        List<TeacherGetBook> teacherGetBooks=teacherGetBookMapper.selectTeacherGetBookByUserId(getAllBook.getUserId());
        for(TeacherGetBook teacherGetBook:teacherGetBooks){
            teacherGetBook.setGetNum(1);
        }
        for(Map<String,Integer> map:getAllBook.getTeacherMap()){

                Integer key = map.get("textbookId");
                Integer value = map.get("textbookNum");
                for(TeacherGetBook teacherGetBook:teacherGetBooks){
                    if(key==teacherGetBook.getTextbookId()) teacherGetBook.setGetNum(value);
                }

        }
        for(TeacherGetBook teacherGetBook:teacherGetBooks){
            teacherGetBookMapper.updateTeacherGetBook(teacherGetBook);
            Textbook textbook=textbookMapper.findById(teacherGetBook.getTextbookId());
            textbook.setStock(textbook.getStock()-teacherGetBook.getGetNum());
            textbookService.updateTextbook(textbook);
        }

        return AjaxResult.success();
    }

}
